package com.obixlabs.security.credentialsstore.server.dao.ldapimpl;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import com.obixlabs.security.credentialsstore.server.dao.ldapimpl.config.LDAPConnectionInfo;

public class LDAPContextFactory 
{	
	public static DirContext createLDAPContext(	LDAPConnectionInfo connectionInfo, 
											String principal, String credentials) 
											throws NamingException
	{
		DirContext result;		

    	Properties props = new Properties();
    	props.put(Context.INITIAL_CONTEXT_FACTORY, 
    					"com.sun.jndi.ldap.LdapCtxFactory");
    	props.put(Context.PROVIDER_URL, connectionInfo.getProviderURL());

    	props.put(Context.SECURITY_AUTHENTICATION, connectionInfo.getAuthenticationType());
    	props.put(Context.SECURITY_PRINCIPAL, principal);
    	props.put(Context.SECURITY_CREDENTIALS,credentials);

    	result = new InitialDirContext(props);    	
    	return result; 
	}
	
	
}
